﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    public class DepartMentController : Controller
    {
        //
        // GET: /DepartMent/

        public ActionResult DepartMentIndex()
        {
            ViewData["PriorNo"] = GetDepartMent();
            return View();
        }


        public ActionResult DepartMentEdit(string Id, int? ParentId)
        {
            ParentId = ParentId.HasValue ? ParentId : 0;
            DepartMentInfo info = new DepartMentInfo();
            DepartMentVM goodsVM = new DepartMentVM();
            goodsVM.Dep_ID = 0;
            goodsVM.DeptLevel = 0;
            int id = 0;
            if ((Id != null) && (Id != ""))
            {
                id = int.Parse(Id);
            }
            if (id > 0)
            {
                string Condition = " and Dep_ID = " + id;
                info = info.GetDepartMentInfo(Condition);
                if (info != null)
                {
                    Mapper.CreateMap<DepartMentInfo, DepartMentVM>();
                    goodsVM = Mapper.Map<DepartMentInfo, DepartMentVM>(info);
                }
                else
                {
                    goodsVM = new DepartMentVM();

                }
            }
            // 数据
            else if ((ParentId != null) && (ParentId != 0))
            {
                goodsVM = new DepartMentVM();
                goodsVM.PriorNo = int.Parse(ParentId.ToString());
            }

            return View(goodsVM);
        }



        private DepartMentInfo dminfo = new DepartMentInfo(true);
        /// <summary>
        /// 获取根目录
        /// </summary>
        /// <returns></returns>
        public ActionResult GetAllDepartMent()
        {
            IList<DepartMentInfo> dmlist = dminfo.GetAllDepartment("and Priorno = 0");
            List<JsonTreeNode> nodes = new List<JsonTreeNode>();
            foreach (var item in dmlist)
            {
                JsonTreeNode cnode = new JsonTreeNode();
                cnode.id = item.Dep_ID.ToString();
                cnode.text = item.DeptName;
                cnode.value = item.Dep_No == null ? "" : item.Dep_No;
                cnode.hasChildren = false;
                cnode.classes = "system";
                cnode.isexpand = false;
                cnode.complete = true;
                AddChildNode(cnode);
                nodes.Add(cnode);
            }
            return Json(nodes);
        }

        public void AddChildNode(JsonTreeNode node)
        {
            int pid = Convert.ToInt32(node.id);
            IList<DepartMentInfo> mlist =
                dminfo.GetAllDepartment("").Where(minfo => minfo.PriorNo == pid).ToList();
            if (mlist != null)
            {
                foreach (var Childitem in mlist)
                {
                    node.hasChildren = true;
                    JsonTreeNode nodeChild = new JsonTreeNode();
                    nodeChild.id = Childitem.Dep_ID.ToString();
                    nodeChild.text = Childitem.DeptName;
                    nodeChild.value = Childitem.Dep_No == null ? "" : Childitem.Dep_No;
                    nodeChild.hasChildren = false;
                    nodeChild.isexpand = true;
                    AddChildNode(nodeChild);
                    node.ChildNodes.Add(nodeChild);
                }
            }
        }
        /// <summary>
        /// 保存部门信息
        /// </summary>
        /// <param name="dvm"></param>
        /// <returns></returns>
        public ActionResult SaveDepartMent(DepartMentVM dvm, int? Id, int? ParentId)
        {
            int flag = 0;
            DepartMentInfo depInfo = new DepartMentInfo();
            if (ModelState.IsValid)
            {
                if (dvm != null)
                {
                    Mapper.CreateMap<DepartMentVM, DepartMentInfo>();
                    depInfo = Mapper.Map<DepartMentVM, DepartMentInfo>(dvm);
                    if ((ParentId != null))
                    {
                        depInfo.PriorNo = Convert.ToInt32(ParentId);
                    }

                    flag = depInfo.SaveDepartMent(depInfo);
                }
            }
            return Json(flag);
        }
        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <param name="deptid"></param>
        /// <returns></returns>
        public ActionResult DeleteDepartMent(string DepartIds)
        {

            bool flag = true;
            if ((DepartIds != null) && (DepartIds != ""))
            {
                string[] ids = DepartIds.Split(',');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (ids[i].ToString() != "")
                    {
                        dminfo.DeleteDepartMent(ids[i].ToString());
                    }
                }

            }
            return Json(flag);
        }
        /// <summary>
        /// 获取一条部门信息
        /// </summary>
        /// <param name="deptid"></param>
        /// <returns></returns>
        public ActionResult GetOneDepartMent(string deptid)
        {
            DepartMentInfo dmi = dminfo.GetDepartMentInfo("and Dep_ID = " + deptid);
            return Json(dmi);
        }
        public SelectList GetDepartMent()
        {
            IList<DepartMentInfo> dmlist = new List<DepartMentInfo>();
            dmlist.Add(new DepartMentInfo() { Dep_ID = 0, DeptName = "根节点" });
            dmlist = dmlist.Concat<DepartMentInfo>(dminfo.GetAllDepartment("")).ToList();
            return new SelectList(dmlist, "Dep_ID", "DeptName");
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pVM"></param>
        /// <returns></returns>
        public ActionResult GetDeparmentList(PageVM pVM)
        {
            int intParentId = 0;
            if (pVM.key.ToString() != "")
            {
                intParentId = int.Parse(pVM.key.ToString());
                if (intParentId == 0)
                {
                    //return Json(true);
                }
            }
            string strCondtion = "and Priorno = " + intParentId;
            int groupCount = dminfo.GetDepartMentCount(strCondtion);
            IList<DepartMentInfo> list = dminfo.GetAllDepartment(strCondtion, pVM.rp, pVM.page);
            JsonFlexiGridData flexigridObject = new JsonFlexiGridData();
            flexigridObject.page = pVM.page;
            flexigridObject.total = groupCount;
            List<string> v;
            foreach (DepartMentInfo ddi in list)
            {
                v = new List<string>();
                //v.Add("<input type=\"checkbox\" />");
                v.Add(ddi.Dep_ID.ToString());
                v.Add(ddi.Dep_No);
                v.Add(ddi.DeptName.ToString());
                v.Add(ddi.PriorNo.ToString());

                FlexiGridRow cell = new FlexiGridRow()
                {
                    id = ddi.Dep_ID.ToString(),
                    cell = v
                };
                flexigridObject.rows.Add(cell);
            }
            return Json(flexigridObject);
        }

    }
}
